﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using BrokerBase.Lib.Person;
using BrokerBase.Lib.Query;
using BrokerBase.Lib.Core;
using Broker.View.Comm;
using Telerik.Windows.Controls;

namespace Broker.View.PersonView
{
    /// <summary>
    /// PersonListPage.xaml 的交互逻辑
    /// </summary>
    public partial class PersonListPage 
    {
        private const int PAGE_SIZE = 15;

        private PagingQuery pq;

        private string hqlBase = "from Person as p ";//默认hql

        public PersonListPage()
        {

            InitializeComponent();
            dpList.PageSize = PAGE_SIZE;
            Loaded += new RoutedEventHandler(PersonListPage_Loaded);
        }

        public ListPageModel PageModel { get; set; }

        public IPerson SelectedItem { get; set; }

        public RadWindow Owner { get; set; }

        void PersonListPage_Loaded(object sender, RoutedEventArgs e)
        {
            dpList.PageSize = PAGE_SIZE;
            SetButtonVisibility();

            LoadAttentionDescriptions();

            if (PageModel == ListPageModel.Select)
            {
                BuildQuery();
                Query();
            }
        }

        private void SetButtonVisibility()
        {
            if (PageModel == ListPageModel.List)
            {
                btnConfirm.Visibility = Visibility.Collapsed;
                btnExit.Visibility = Visibility.Collapsed;
            }
            else
            {
                btnConfirm.Visibility = Visibility.Visible;
                btnExit.Visibility = Visibility.Visible;
            }
        }

        public IPerson SelectByHQL(string hql, ContentControl owner, double height, double width)
        {
            if (!string.IsNullOrEmpty(hql))
                hqlBase = hql;



            PageModel = ListPageModel.Select;

            RadWindow w = new RadWindow();
            w.Header = "客户资料";
            w.Owner = owner;
            w.WindowStartupLocation = WindowStartupLocation.CenterOwner;
            w.Height = height;
            w.Width = width;

            Frame f = new Frame();

            f.Content = this;

            w.Content = f;
            this.Owner = w;
            w.ShowDialog();

            return SelectedItem;
        }

        private void btnQuery_Click(object sender, RoutedEventArgs e)
        {
            BuildQuery();
            Query();
        }

        private void dpList_PageIndexChanged(object sender, Telerik.Windows.Controls.PageIndexChangedEventArgs e)
        {
            if (pq != null)
            {
                pq.FirstResult = e.NewPageIndex * PAGE_SIZE;

                Query();
            }
        }

        private void btnAdd_Click(object sender, RoutedEventArgs e)
        {
            IPerson person = new Person();

            person.Birthday = LibGlobal.GetClientCfg().DefaultBirthday;
            person.Sex = SexType.Male;
            person.Attention = 3;
            PersonForm frm = new PersonForm(person,ReQuery);

            frm.WindowStartupLocation = WindowStartupLocation.CenterOwner;
            frm.Owner = LibGlobal.MainWin;
            frm.ShowDialog();
        }

        private void btnEdit_Click(object sender, RoutedEventArgs e)
        {
            View();
        }

        private void benDel_Click(object sender, RoutedEventArgs e)
        {
            IPerson p = gvPerson.SelectedItem as IPerson;

            if (p != null)
            {
                if (MessageBox.Show(string.Format("是否要删除客户:{0}?", p.Name), "提示",MessageBoxButton.YesNo) == MessageBoxResult.Yes)
                {
                    try
                    {
                        IBaseService<Person> svr = new BaseService<Person>(LibGlobal.HbmSessionFactory.GetCurrentSession());

                        svr.Delete(p as Person);

                        Query();
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message, "提示");
                    }
                }
            }
        }


        private void ViewPerson()
        {
            IPerson p = gvPerson.SelectedItem as IPerson;

            if (p != null)
            {
                PersonForm frm = new PersonForm(p);

                frm.WindowStartupLocation = WindowStartupLocation.CenterOwner;
                frm.Owner = LibGlobal.MainWin;
                frm.ShowDialog();
            }
        }


        private void BuildQuery()
        {
            pq = new PagingQuery();

            string where = "where 1=1";

            if (!string.IsNullOrEmpty(txtName.Text))
            {
                where += " and p.Name Like '%"+txtName.Text.Trim()+"%'";
            }

            if (dateStart.SelectedValue != null)
            {
                where +=string.Format(" and p.Birthday >='{0}'",dateStart.SelectedValue.Value.ToString("yyyy-MM-dd"));
            }

            if (dateEnd.SelectedDate !=null)
            {
                where += string.Format(" and p.Birthday <='{0}'", dateEnd.SelectedValue.Value.ToString("yyyy-MM-dd"));
            }

            if (!string.IsNullOrEmpty(txtTel.Text))
            {
                string tel = txtTel.Text.Trim();
                where += " and (p.Mobile Like '%" + tel + "%' or p.HomePhone Like '%" + tel +"%' or p.WorkPhone Like '%"+ tel +"%')";
            }

            AttentionDescription ad = cmbAttention.SelectedItem as AttentionDescription;

            if (ad != null && ad.Value != 0)
            {
                where += " and p.Attention="+ad.Value;
            }

            pq.HQL = hqlBase + where +" order by p.Id DESC";
            pq.TotalHQL = "select count(*) " + pq.HQL;
        }

        private void Query()
        {
            BaseService<Person> svr = new BaseService<Person>(LibGlobal.HbmSessionFactory.GetCurrentSession());

            long total = 0;

            IList<Person> ls = svr.GetListByHQLWithPaging(pq.HQL, pq.TotalHQL, pq.FirstResult, PAGE_SIZE, out total);
            
            //gvPerson.ItemsSource = null;
            gvPerson.ItemsSource = ls;
        }

        private void ReQuery()
        {
            BuildQuery();
            Query();
        }

        private void btnExit_Click(object sender, RoutedEventArgs e)
        {
            SelectedItem = null;

            Owner.Close();
        }

        private void btnConfirm_Click(object sender, RoutedEventArgs e)
        {
            IPerson p = gvPerson.SelectedItem as IPerson;

            if (p != null)
            {
                SelectedItem = p;

                Owner.Close();
            }
        }

        private void gvPerson_MouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            if (PageModel == ListPageModel.Select)
            {
                IPerson p = gvPerson.SelectedItem as IPerson;

                if (p != null)
                {
                    SelectedItem = gvPerson.SelectedItem as IPerson;
                    Owner.Close();
                }
            }
            else
            {
                View();
            }
        }

        private void View()
        {
            IPerson p = gvPerson.SelectedItem as IPerson;

            if (p != null)
            {
                PersonForm frm = new PersonForm(p);

                frm.WindowStartupLocation = WindowStartupLocation.CenterOwner;
                frm.Owner = LibGlobal.MainWin;

                frm.ShowDialog();
            }
        }

        private void gvHumanRelationType_MouseDoubleClick(object sender, MouseButtonEventArgs e)
        {

        }

        private void LoadAttentionDescriptions()
        {
            AttentionDescription ad0 = new AttentionDescription() { Name = "全部", Value = 0 };
            AttentionDescription ad1 = new AttentionDescription() { Name = "1星", Value = 1 };
            AttentionDescription ad2 = new AttentionDescription() { Name = "2星", Value = 2 };
            AttentionDescription ad3 = new AttentionDescription() { Name = "3星", Value = 3 };
            AttentionDescription ad4 = new AttentionDescription() { Name = "4星", Value = 4 };
            AttentionDescription ad5 = new AttentionDescription() { Name = "5星", Value = 5 };

            AttentionDescription[] ads = new AttentionDescription[6] { ad0, ad1, ad2, ad3, ad4, ad5 };

            cmbAttention.ItemsSource = ads;
        }
    }

    class AttentionDescription
    {
        public string Name { get; set; }

        public int Value { get; set; }
    }
}
